//
//  FurnitureActionView.m
//  Model
//
//  Created by zeb-apple on 2017/5/31.
//  Copyright © 2017年 YDJH. All rights reserved.
//  指令集view

#import "FurnitureActionView.h"
#import "ActionCollectionCell.h"
#import "HZFurniture.h"
#import "HZCommand.h"
#import "HZFurniture_Header.h"

@interface FurnitureActionView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) HZFurniture *furniture; //家具
@property (nonatomic, strong) NSMutableArray *dataArray; //数据源

@property (nonatomic, copy) ActionClickBlock actionClickBlock;

@end

@implementation FurnitureActionView


- (instancetype)initWithFrame:(CGRect)frame actionClickBlock:(ActionClickBlock)actionClickBlock {
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        self.actionClickBlock = actionClickBlock;
        
        [self initView];
    }
    return self;
}
- (void)initView {
    [self addSubview:self.collectionView];
}

- (UICollectionView *)collectionView {
    if (!_collectionView ) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        NSInteger margin = 5;
        layout.itemSize = CGSizeMake((SCREEN_WIDTH-30)/5 , (SCREEN_WIDTH-30)/5);
        layout.minimumInteritemSpacing = margin;
        layout.minimumLineSpacing = margin;
        layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollsToTop = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor clearColor];
        [_collectionView registerClass:[ActionCollectionCell class] forCellWithReuseIdentifier:ActionsCollectionViewCellID];
    }
    return _collectionView;
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    //替换数据源
    return self.dataArray.count;
}

- ( UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ActionCollectionCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:ActionsCollectionViewCellID forIndexPath:indexPath];
    HZCommand *command = self.dataArray[indexPath.row];
    [cell configureCellWithData:command.commandName withCallBack:^(NSString *type) {
        
    }];
    return cell;
    
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((SCREEN_WIDTH-30)/4 ,(SCREEN_WIDTH-30)/5);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self dissmiss];
    HZCommand *command = self.dataArray[indexPath.row];
    self.actionClickBlock(self.furniture, command);

}
#pragma mark
#pragma mark reloadcollection
- (void)reloadCollectionWithFurniture:(HZFurniture *)furniture {
    self.dataArray = (NSMutableArray *)furniture.actionCollection;
    self.furniture = furniture;
    [self.collectionView reloadData];
    [self.collectionView setContentOffset:CGPointMake(0, 0) animated:NO];
}

- (void)show {
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(0, SCREEN_HEIGHT-Mine_H, SCREEN_WIDTH, Mine_H);
    }];
}
- (void)dissmiss {
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, Mine_H);
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
